#!/usr/bin/env python3
'''ssh 权限自动化开通'''
__author__="dk"

from flask import render_template
from conf import config
from conf.config import *
from app.app import APP
from flask import request
from flask import Flask
import time
from flask_sqlalchemy import SQLAlchemy
from flask import send_file

'''
app = APP(config)

db = app.get_db()
api = app.get_api()
'''

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']=\
"mysql+pymysql://root:123123@localhost/ssh"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)

class user(db.Model):
    __tablename__='user'
    user_id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String)
    passwd = db.Column(db.String)
    department = db.Column(db.String)
    manager = db.Column(db.String)
    date = db.Column(db.Date)

@app.route('/registerdb', methods=['POST'])
def user_in():
    try:
        user_info = user(username=request.form['name'], passwd=request.form['pwd'], department=request.form['department'],
manager=request.form['leader'], date=time.strftime('%Y%m%d'))
        db.session.add(user_info)
        db.session.commit()
    except :
        return """用户名不能重复，请重新输入   <a href='/register'>点击重新输入</a>"""
    return "注册成功"

#首页
@app.route('/')
def index():
    return render_template("index.html")

#路由到登录页面
@app.route('/login')
def login():
    return render_template("login.html")
    #return send_file("../templates/login.html")

#路由到注册页面
@app.route('/register')
def register():
    return render_template('register.html')

#用于测试
@app.route('/aa')
def root():
    return app.root_path

@app.route('/user_auth', methods =['POST'])
def user_auth():
    user_name = request.form['username']
    password  = request.form['pwd']
    u_info = user.query.filter_by(username =user_name).first()
    if u_info:
        if u_info.passwd == password:
            return 'userorder.html'
        else:
            return "<br><br><h2>          用户密码错误,请重新输入  <a href ='/login'>点击重新登录</a></h2>"
    else:
        return "<br><br><h2>无次用户名,请重新输入  <a href ='/login'>点击重新登录</a></h2>"



if __name__ == '__main__':
    app.run(host=HOST,port=listen_port,debug=True,threaded=True)